import itchat
import math
import PIL.Image as Image


def _pillow():
    each_size = int(math.sqrt(float(640 * 640) / 280))
    lines = int(640 / each_size)
    image = Image.new('RGBA', (640, 640))
    x = 0
    y = 0
    for i in range(0, 280):
        try:
            img = Image.open('/home/eric/image' + "/" + str(i) + ".jpg")
        except IOError:
            print("Error:" + str(i))
        else:
            img = img.resize((each_size, each_size), Image.ANTIALIAS)
            image.paste(img, (x * each_size, y * each_size))
            x += 1
            if x == lines:
                x = 0
                y += 1

    if len(image.split()) == 4:
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))

    image.save('/home/eric/image' + "/" + "all.jpg")
    itchat.send_image('/home/eric/image' + "/" + "all.jpg", 'filehelper')


_pillow()
